<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>新增页面</title>
	<link rel="stylesheet" href="../../../component/pear/css/pear.css" />
</head>
<body>
<form class="layui-form" action="">
	<div class="mainBox">
		<div class="main-container">

			<div class="layui-form-item layui-inline">
				<label class="layui-form-label">课程名称</label>
				<div class="layui-input-inline">
					<input type="text" name="cname" placeholder="" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item layui-inline">
				<label class="layui-form-label">课程数</label>
				<div class="layui-input-inline">
					<input type="number" name="cnums" placeholder="" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item layui-inline">
				<label class="layui-form-label">概述</label>
				<div class="layui-input-inline">
					<input type="text" name="gaishu" placeholder="" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-card-body">面向群体
					<textarea name="qunti" id="qunti" cols="30" rows="10"></textarea>
				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-card-body">课程内容
					<textarea name="neirong" id="neirong" cols="30" rows="10"></textarea>
				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-card-body">方案
					<textarea name="fangan" id="fangan" cols="30" rows="10"></textarea>
				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-card-body">优势
					<textarea name="youshi" id="youshi" cols="30" rows="10"></textarea>
				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-card-body">师资
					<textarea name="shizi" id="shizi" cols="30" rows="10"></textarea>
				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-card-body">设备
					<textarea name="shebei" id="shebei" cols="30" rows="10"></textarea>
				</div>
			</div>
			<div class="layui-form-item layui-inline">
				<label class="layui-form-label">是否显示</label>
				<div class="layui-input-inline">
					<input type="radio" name="isvalid" value="true" title="是" checked>
					<input type="radio" name="isvalid" value="false" title="否" >
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">封面图片</label>
				<div class="layui-input-block">
					<input type="text"   name="coverimg" lay-verify="idcardpic" autocomplete="off" placeholder="照片" id="idcardpic"
						   class="layui-input">
					<input class="btn-upload file-up" style="display: inline-block;" type="file"   name="edit" id="edit" value="上传">
				</div>
				<div class="layui-input-block">
					<img id="preview" height="200px" width="300px" src="#" alt="没有选择"/>
					<span id="demoText"></span>
				</div>
			</div>
		</div>
	</div>
	<div class="bottom">
		<div class="button-container">
			<button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
					lay-filter="user-save">
				<i class="layui-icon layui-icon-ok"></i>
				提交
			</button>
			<button type="reset" class="pear-btn pear-btn-sm">
				<i class="layui-icon layui-icon-refresh"></i>
				重置
			</button>
		</div>
	</div>
</form>
<script src="../../../component/layui/layui.js"></script>
<script src="../../../component/pear/pear.js"></script>
<script>
	layui.use(['form', 'jquery','upload','tinymce'], function() {
		var BASE_URL="/shengyin/";
		let form = layui.form;
		let $ = layui.jquery;
		var upload = layui.upload;
		var tinymce = layui.tinymce;
		var qunti = tinymce.render({
			elem: "#qunti",
			height: 400,
			automatic_uploads: true,
			images_upload_url: BASE_URL+"upload.do",
			images_upload_base_path: BASE_URL,
		});
		var neirong = tinymce.render({
			elem: "#neirong",
			height: 400,
			automatic_uploads: true,
			images_upload_url: BASE_URL+"upload.do",
			images_upload_base_path: BASE_URL,
		});
		var fangan = tinymce.render({
			elem: "#fangan",
			height: 400,
			automatic_uploads: true,
			images_upload_url: BASE_URL+"upload.do",
			images_upload_base_path: BASE_URL,
		});
		var youshi = tinymce.render({
			elem: "#youshi",
			height: 400,
			automatic_uploads: true,
			images_upload_url: BASE_URL+"upload.do",
			images_upload_base_path: BASE_URL,
		});
		var shizi = tinymce.render({
			elem: "#shizi",
			height: 400,
			automatic_uploads: true,
			images_upload_url: BASE_URL+"upload.do",
			images_upload_base_path: BASE_URL,
		});
		var shebei = tinymce.render({
			elem: "#shebei",
			height: 400,
			automatic_uploads: true,
			images_upload_url: BASE_URL+"upload.do",
			images_upload_base_path: BASE_URL,
		});
		$.ajaxSetup({
			headers:{
				'Authorization':window.sessionStorage.getItem("token")
			}
		})
		form.on('submit(user-save)', function(data) {
			data.field.qunti=qunti.getContent();
			data.field.neirong=neirong.getContent();
			data.field.fangan=fangan.getContent();
			data.field.youshi=youshi.getContent();
			data.field.shizi=shizi.getContent();
			data.field.shebei=shebei.getContent();
			$.ajax({
				url: window.BASEPATH+'subject/add',
				data: data.field,
				type: 'post',
				success: function(result) {
					if (result.code==0) {
						layer.msg(result.msg, {
							icon: 1,
							time: 1000
						}, function() {
							parent.layer.close(parent.layer.getFrameIndex(window
									.name)); //关闭当前页
							parent.layui.table.reload("user-table");
						});
					} else {
						layer.msg(result.msg, {
							icon: 2,
							time: 1000
						});
					}
				}
			})
			return false;
		});
		var uploadInst = upload.render({
			elem: '#edit'
			,url: window.BASEPATH+'upload2' //改成您自己的上传接口
			,accept: 'file'
			//,accept: '*'
			,auto: true  //选择文件后不自动上传

			,choose: function(obj){
				//预读本地文件示例，不支持ie8
				obj.preview(function(index, file, result){
					$('#preview').attr('src', result); //图片链接（base64）
				});
			}
			,before: function(obj){
				layer.msg('上传中', {icon: 16, time: 0});
			}
			,done: function(res){
				//如果上传失败
				if(res.code > 0){
					return layer.msg('上传失败');
				}
				$("#idcardpic").val(res.data);
				//上传成功的一些操作
				layer.msg("信息上传成功！",{time: 3000});
			}
			,error: function(){
				//演示失败状态，并实现重传
				var demoText = $('#demoText');
				demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
				demoText.find('.demo-reload').on('click', function(){
					uploadInst.upload();
				});
			}

		});
	})
</script>
<script>
</script>
</body>
</html>
